﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using YourWorld.Common;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Presentation.Commands;
using MainGameModule.Views;
using BusinessObjects;
using MainGameModule.Models;
using YourWorld.Common.Infrastucture.Events;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace MainGameModule.ViewModels
{
    public class JoinSupportQuestViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;
        private IGameModel gameModel;

        private string headerText;
        private Event quest;
        private Player sender;
        private string otherPlayerName;
        private string comments;
        private string greeting;

        private bool isSupportQuestRequest;
        private bool isJoinQuestRequest;
        private bool isQuestResponse;
        private bool canBuildObject;
        private bool isCommentsVisible;
        private ObservableCollection<Player> members;

        public JoinSupportQuestViewModel( IRegionManager regionManager, IUnityContainer container, IGameModel gameModel )
        {
            this.regionManager = regionManager;
            this.container = container;
            this.gameModel = gameModel;

            this.CloseCommand = new DelegateCommand<string>( this.CloseView );
            this.JoinCommand = new DelegateCommand<object>( this.JoinQuest );
            this.SendMessageCommand = new DelegateCommand<object>( this.SendMessage );
            this.BuildCommand = new DelegateCommand<object>( this.BuildQuest );
        }

        public ICommand CloseCommand
        {
            get;
            private set;
        }

        public ICommand SendMessageCommand
        {
            get;
            private set;
        }

        public ICommand JoinCommand
        {
            get;
            private set;
        }

        public ICommand BuildCommand
        {
            get;
            private set;
        }

        public string HeaderText
        {
            get
            {
                return this.headerText;
            }
            set
            {
                if ( this.headerText == value )
                    return;

                this.headerText = value;
                this.NotifyPropertyChanged( "HeaderText" );
            }
        }

        public Event Quest
        {
            get
            {
                return this.quest;
            }
            set
            {
                if ( this.quest == value )
                    return;

                this.quest = value;
                this.NotifyPropertyChanged( "Quest" );
            }
        }

        public Player Sender
        {
            get
            {
                return this.sender;
            }
            set
            {
                if ( this.sender == value )
                    return;

                this.sender = value;
                this.NotifyPropertyChanged( "Sender" );
            }
        }

        public string Comments
        {
            get
            {
                return this.comments;
            }
            set
            {
                if ( this.comments == value )
                    return;

                this.comments = value;
                this.NotifyPropertyChanged( "Comments" );
            }
        }

        public string Greeting
        {
            get
            {
                return this.greeting;
            }
            set
            {
                if ( this.greeting == value )
                    return;

                this.greeting = value;
                this.NotifyPropertyChanged( "Greeting" );
            }
        }

        public string OtherPlayerName
        {
            get
            {
                return this.otherPlayerName;
            }
            set
            {
                if ( this.otherPlayerName == value )
                    return;

                this.otherPlayerName = value;
                this.NotifyPropertyChanged( "OtherPlayerName" );
            }
        }

        public int OtherPlayerId
        {
            get;
            set;
        }

        public bool IsSupportQuestRequest
        {
            get
            {
                return this.isSupportQuestRequest;
            }
            set
            {
                if ( this.isSupportQuestRequest == value )
                    return;

                this.isSupportQuestRequest = value;
                this.NotifyPropertyChanged( "IsSupportQuestRequest" );
            }
        }

        public bool IsJoinQuestRequest
        {
            get
            {
                return this.isJoinQuestRequest;
            }
            set
            {
                if ( this.isJoinQuestRequest == value )
                    return;

                this.isJoinQuestRequest = value;
                this.NotifyPropertyChanged( "IsJoinQuestRequest" );
            }
        }

        public bool IsQuestResponse
        {
            get
            {
                return this.isQuestResponse;
            }
            set
            {
                if ( this.isQuestResponse == value )
                    return;

                this.isQuestResponse = value;
                this.NotifyPropertyChanged( "IsQuestResponse" );
            }
        }

        public bool IsCommentsVisible
        {
            get
            {
                return this.isCommentsVisible;
            }
            set
            {
                if ( this.isCommentsVisible == value )
                    return;

                this.isCommentsVisible = value;
                this.NotifyPropertyChanged( "IsCommentsVisible" );
            }
        }

        public bool CanBuildObject
        {
            get
            {
                return this.canBuildObject;
            }
            set
            {
                if ( this.canBuildObject == value )
                    return;

                this.canBuildObject = value;
                this.NotifyPropertyChanged( "CanBuildObject" );
            }
        }

        public void Initialize()
        {
            this.IsCommentsVisible = false;
            this.IsJoinQuestRequest = false;
            this.IsQuestResponse = false;
            this.IsSupportQuestRequest = false;
            this.CanBuildObject = false;
            this.Members.Clear();
        }

        public ObservableCollection<Player> Members
        {
            get
            {
                if ( this.members == null )
                    this.members = new ObservableCollection<Player>();

                return this.members;
            }
            set
            {
                if ( this.members == value )
                    return;

                this.members = value;
                this.NotifyPropertyChanged( "Members" );
            }
        }

        private void CloseView( string notUsed )
        {
            PlayerStatusManager.Instance.ContinueGame();
            this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate( this.container.Resolve<EventsJoinOrSupportView>() );
        }

        private void SendMessage( object o )
        {
            EventFeedback ef = new EventFeedback()
            {
                Feedback = this.Comments,
                GameEvent = this.Quest,
                SenderId = this.OtherPlayerId
            };

            this.gameModel.SupportEvent( ef );
            this.CloseView( String.Empty );
        }

        private void JoinQuest( object o )
        {
            EventFeedback ef = new EventFeedback()
            {
                Feedback = this.Comments,
                GameEvent = this.Quest,
                SenderId = this.OtherPlayerId
            };

            this.gameModel.JoinEvent( ef );
            this.CloseView( String.Empty );
        }

        private void BuildQuest( object o )
        {
            this.CloseView( String.Empty );

            Quest quest = new Quest();
            quest.Id = this.Quest.Id;

            YourWorld.Common.GreenWorldEntitiesService.RegionObject ro = RegionObjectsRepository.Instance.RegionObjects[quest.Id];
            Debug.Assert( ro != null );

            quest.Width = ro.Width ?? 100;
            quest.Height = ro.Height ?? 100;
            quest.Description = ro.Description;
            quest.Image = this.Quest.Image;

            quest.QuestType = ( BuildQuestTypes )Enum.Parse( typeof( BuildQuestTypes ), ro.Template, true );  

            string showText = "Great Work! You have all the required badges! ";
            showText += "In order to choose a place where the object will be build, click ok and click once with the right mouse button on the desired place.";
           
            EventService.Aggregator.GetEvent<ShowInfoBoxEvent>().Publish( showText );
            EventService.Aggregator.GetEvent<BuildQuestEvent>().Publish( quest );
        }
    }
}
